package com.qdlc.p2p.biz.service.impl;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.qdlc.p2p.biz.service.BorrowBespeakService;
import com.qdlc.p2p.common.util.StringUtil;
import com.qdlc.p2p.dal.common.model.jpa.OrderFilter.OrderType;
import com.qdlc.p2p.dal.common.model.jpa.PageDataList;
import com.qdlc.p2p.dal.common.model.jpa.QueryParam;
import com.qdlc.p2p.dal.common.model.jpa.SearchFilter;
import com.qdlc.p2p.dal.common.model.jpa.SearchFilter.Operators;
import com.qdlc.p2p.dal.dao.BorrowBespeakDao;
import com.qdlc.p2p.dal.dto.BorrowBespeak;

@Service("borrowBespeakService")
public class BorrowBespeakServiceImpl implements BorrowBespeakService {

    @Resource
    private BorrowBespeakDao borrowBespeakDao;

    @Override
    public PageDataList<BorrowBespeak> borrowBespeakList(
            BorrowBespeak borrowBespeak, int page, int size, String searchName) {
        QueryParam param = QueryParam.getInstance();
        param.addPage(page, size);
        if (borrowBespeak != null) {
            if (!StringUtil.isBlank(searchName)) {
                SearchFilter orFilter1 = new SearchFilter("companyName", Operators.LIKE, searchName.trim());
                SearchFilter orFilter2 = new SearchFilter("name", Operators.LIKE, searchName.trim());
                param.addOrFilter(orFilter1, orFilter2);
            } else {
                if (borrowBespeak.getStatus() != 0) {
                    param.addParam("status", borrowBespeak.getStatus());
                } else {
                    param.addParam("status", 0);
                }
                if (!StringUtil.isBlank(borrowBespeak.getCompanyName())) {
                    param.addParam("companyName", Operators.LIKE, borrowBespeak.getCompanyName().trim());
                }
                if (!StringUtil.isBlank(borrowBespeak.getName())) {
                    param.addParam("name", Operators.LIKE, borrowBespeak.getName().trim());
                }
                if (!StringUtil.isBlank(borrowBespeak.getTelephone())) {
                    param.addParam("telephone", Operators.LIKE, borrowBespeak.getTelephone().trim());
                }

            }
            param.addOrder(OrderType.DESC, "addTime");
        }
        PageDataList<BorrowBespeak> pageDataList = borrowBespeakDao.findPageList(param);
        return pageDataList;
    }

    @Override
    public BorrowBespeak find(long id) {
        return borrowBespeakDao.find(id);
    }

    @Override
    public void borrowBespeakEdit(BorrowBespeak borrowBespeak) {
        borrowBespeakDao.update(borrowBespeak);
    }
}
